package com.yx.changdao.web.template;

import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.UploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: <br/>
 * 文件 图片上传 示例Demo
 * <p>
 * <br/>
 * @Auther: zhangqi
 * @create 2019/11/19 9:17
 */
@RestController
// 设置字符集
@RequestMapping(value = "/demo/upload/", produces = {"application/json;charset=UTF-8"})
public class UploadController {


    /**
     * 上传文件  格式不限
     *
     * @return
     */
    @PostMapping(value = "/file")
    public Result uploadFile(@RequestParam("file") MultipartFile file) throws AlertException {
        String url = UploadUtils.uploadFile(file);
        if (StringUtils.isBlank(url)) {
            Result.error();
        }
        System.out.println("url = " + url);
        return Result.ok("成功");
    }

    /**
     * 上传图片 格式限制只能是图片格式
     *
     * @return
     */
    @PostMapping(value = "/image")
    public Result uploadImage(@RequestParam("image") MultipartFile image) throws AlertException {
        String url = UploadUtils.uploadImage(image);
        if (StringUtils.isBlank(url)) {
            Result.error();
        }
        System.out.println("url = " + url);
        return Result.ok("成功");
    }


}
